package com.ea.report;

import com.ea.common.SA.FaceRegFeign;
import com.ea.common.SA.SAFeign;
import com.ea.common.auth.AdminFeign;
import com.ea.common.gateway.GatewayFeign;
import com.ea.common.report.ReportFeign;
import com.ea.report.config.StorageProperties;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;

@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = "com.ea")
@EnableConfigurationProperties(StorageProperties.class)
@EnableFeignClients(basePackageClasses = {AdminFeign.class, ReportFeign.class, GatewayFeign.class, SAFeign.class, FaceRegFeign.class})
public class ReportApplication {
    private static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        context = SpringApplication.run(ReportApplication.class, args);
    }


    public static void restart() {
        // 获取原有的参数
        ApplicationArguments args = context.getBean(ApplicationArguments.class);
        String[] sourceArgs = args.getSourceArgs();

        Thread thread = new Thread(() -> {
            try {
                // 关闭当前上下文
                context.close();


                // 重启应用，并传递原有参数
                context = SpringApplication.run(ReportApplication.class, sourceArgs);
            } catch (Exception e) {
                e.printStackTrace(); // 打印异常信息
            }
        });

        thread.setDaemon(false);
        thread.start();
    }
}
